<template>
  <!-- 折线图(曲线) -->
  <div style="width: 100%; height: 100%">
    <div ref="oneLeft" class="multiBeam" />
  </div>
</template>

<script>
import * as echarts from "echarts";
export default {
  name: "Portait",
  props: {
    chartData: {
      type: Object,
    },
    /** 颜色列表 */
    colorList: {
      type: Array,
    },
    /** 盒子边距 */
    positionGrid: {
      type: Object,
      default: () => {
        return {
          top: "20%",
          left: "5%",
          bottom: "40",
          right: "0%",
        };
      },
    },
    /** 折线类别数组 */
    lenged: {
      type: Array,
      default: () => {
        return ["立法申请数量趋势", "立法引用率趋势"];
      },
    },
    /** 折线类别数组显示 */
    lengedshow: {
      type: Boolean,
      default: true,
    },
  },
  data() {
    return {
      chart: null,
      // 柱状图配置项
      scoreOption: {},
    };
  },
  destroyed() {
    window.removeEventListener("resize", this.onResize);
    window.onresize = null;
  },
  mounted() {
    this.draw();
    this.$nextTick(() => {
      //   this.initChart()
      this.chart.resize();
      this.chart = echarts.init(this.$el, "macarons");
      let self = this;
      let chart = this.chart;
      chart.on("click", (params) => {
        console.log(params);
        self.$emit("getLine", params);
      });
    });
  },
  methods: {
    draw() {
      console.log(11, this.chartData, this.lengthData);
      this.chart = echarts.init(this.$refs.oneLeft);
      this.scoreOption = this.options || {
        backgroundColor: "#fff",
        color: this.colorList,
        legend: {
          show: this.lengedshow,
          align: "left",
          right: "0%",
          top: "0%",
          itemWidth: 16,
          itemHeight: 2,
          data: this.legend,
        },
        // title: {
        //   text: '',
        //   textStyle: {
        //     fontSize: 12,
        //     fontWeight: 400
        //   },
        //   left: 'center',
        //   top: '5%'
        // },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            label: {
              show: true,
              backgroundColor: "#fff",
              color: "#556677",
              borderColor: "rgba(0,0,0,0)",
              shadowColor: "rgba(0,0,0,0)",
              shadowOffsetY: 0,
            },
            lineStyle: {
              width: 0,
            },
          },
          backgroundColor: "#fff",
          textStyle: {
            color: "#5c6c7c",
          },
          padding: [10, 10],
          extraCssText: "box-shadow: 1px 0 2px 0 rgba(163,163,163,0.5)",
        },
        grid: this.positionGrid,
        xAxis: [
          {
            type: "category",
            data: this.chartData.xData,
            axisLine: {
              show: true,
              lineStyle: {
                color: "rgb(153,153,153)",
              },
            },
            axisTick: {
              show: false,
            },
            axisLabel: {
              interval: 0,
              textStyle: {
                color: "#666666",
                fontSize: 18,
              },
              //   formatter(val) {
              //     var strs = val.split('') // 字符串数组
              //     var str = ''
              //     for (var i = 0, s; (s = strs[i++]); i++) {
              //       // 遍历字符串数组
              //       str += s
              //       if (!(i % 5)) str += '\n' // 按需要求余
              //     }
              //     return str
              //   },

              // 默认x轴字体大小
              //   fontSize: 12,
              //   left: '5%',
              // margin:文字到x轴的距离
              margin: 20,
            },
            axisPointer: {
              label: {
                show: false,
              },
            },
            // boundaryGap: false
          },
        ],
        yAxis: [
          {
            type: "value",
            minInterval: 5,
            // name: '立法申请数量趋势',
            nameTextStyle: {
              color: "#666",
              fontSize: 14,
              padding: [20, 0, 0, 40],
            },
            axisTick: {
              show: false,
            },
            axisLine: {
              show: false,
              lineStyle: {
                color: "#DCE2E8",
              },
            },
            axisLabel: {
              formatter(value, index) {
                if (value >= 10000 && value < 10000000) {
                  value = value / 10000 + "万";
                } else if (value >= 10000000) {
                  value = value / 10000000 + "千万";
                }
                return value;
              },
              textStyle: {
                color: "#666666",
                fontSize: 18,
              },
            },
            splitLine: {
              show: true,
              lineStyle: {
                color: "rgba(153, 153, 153, .2)",
                type: "dashed",
                width: 1,
              },
            },
          },
          {
            show: false,
            // name: '立法引用率趋势',
            type: "value",
            nameTextStyle: {
              color: "#666",
              fontSize: 14,
              padding: [20, 40, 0, 0],
            },
            axisTick: {
              show: false,
            },
            axisLabel: {
              formatter: "{value}%",
              textStyle: {
                color: "#666666",
                fontSize: 18,
              },
            },
            axisLine: {
              show: false,
              lineStyle: {
                color: "#DCE2E8",
              },
            },
            splitLine: {
              show: false,
            },
          },
        ],
        series: [
          {
            name: this.lenged[0],
            type: "line",
            data: this.chartData.yData[0],
            symbolSize: 1,
            symbol: "circle",
            smooth: true,
            yAxisIndex: 0,
            showSymbol: false,
            lineStyle: {
              width: 3,
              color: this.colorList[0],
              shadowColor: this.colorList[0],
              shadowBlur: 10,
              shadowOffsetY: 10,
            },
            itemStyle: {
              normal: {
                color: `${this.colorList[0]}`,
                borderColor: this.colorList[0],
              },
            },
          },
          {
            name: this.lenged[1],
            type: "line",
            data: this.chartData.yData[1],
            symbolSize: 1,
            symbol: "circle",
            smooth: true,
            yAxisIndex: 0,
            showSymbol: false,
            lineStyle: {
              width: 3,
              color: this.colorList[1],
              shadowColor: this.colorList[1],
              shadowBlur: 10,
              shadowOffsetY: 10,
            },
            itemStyle: {
              normal: {
                color: `${this.colorList[1]}`,
                borderColor: this.colorList[1],
              },
            },
          },
          {
            show: this.lenged[2],
            name: this.lenged[2],
            type: "line",
            data: this.chartData.yData[2],
            symbolSize: 1,
            symbol: "circle",
            smooth: true,
            yAxisIndex: 1,
            showSymbol: false,
            lineStyle: {
              width: 3,
              color: this.colorList[2],
              shadowColor: this.colorList[2],
              shadowBlur: 10,
              shadowOffsetY: 10,
            },
            itemStyle: {
              normal: {
                color: `${this.colorList[2]}`,
                borderColor: this.colorList[2],
              },
            },
          },
          {
            show: this.lenged[3],
            name: this.lenged[3],
            type: "line",
            data: this.chartData.yData[3],
            symbolSize: 1,
            symbol: "circle",
            smooth: true,
            yAxisIndex: 0,
            showSymbol: false,
            lineStyle: {
              width: 3,
              color: this.colorList[3],
              shadowColor: this.colorList[3],
              shadowBlur: 10,
              shadowOffsetY: 10,
            },
            itemStyle: {
              normal: {
                color: `${this.colorList[3]}`,
                borderColor: this.colorList[3],
              },
            },
          },
        ],
      };
      this.chart.setOption(this.scoreOption);
      window.addEventListener("resize", this.onResize);
    },
    onResize() {
      this.chart.resize();
    },
  },
};
</script>
  <style scoped>
.multiBeam {
  width: 100%;
  height: 100%;
}
</style>

